//
//  ViewController.m
//  裁剪（彩色分区）
//
//  Created by MyMac on 15/7/20.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)CAGradientLayer*layer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CAGradientLayer*gradient=[CAGradientLayer layer];//裁剪层
    gradient.startPoint=CGPointMake(0, 0);//表颜色渐变的方向，是个向量，从左上角到右下角
    gradient.endPoint=CGPointMake(1, 1);
    UILabel*label=[[UILabel   alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    label.textColor=[UIColor  redColor];
    label.text=@"我是小码哥";
    label.font=[UIFont boldSystemFontOfSize:30];
    label.font=[UIFont systemFontOfSize:25];
    gradient.frame=label.frame;
    self.layer=gradient;
    [self.view addSubview:label];
    gradient.colors=@[(id)[self randomColor].CGColor,(id)[self randomColor].CGColor,(id)[self randomColor].CGColor];
    [self.view.layer addSublayer:gradient];
    //mask的裁剪原理是按裁剪样本中，透明的裁剪掉，非透明的留下，，在label 中，文字就是非透明的，，，，每个CALayer都有mask属性
 gradient.mask=label.layer;//把遮掩层的mask，也就是裁剪的样本设置为label.layer，label本身是不让他显示的，，只是作为遮掩层的裁剪样本，按label裁剪遮掩层
    //注意，，，设置了这个mask属性后，label将从父视图中移除，他的父视层变为gradient，所以frame也得变，按label为父视图变
    label.frame=gradient.bounds;
//    CADisplayLink*link=[CADisplayLink displayLinkWithTarget:self selector:@selector(lick)];
//    link.frameInterval=2;
//    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    NSTimer*timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(lick) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    
}
-(void)lick
{
   

    self.layer.colors=@[(id)[self randomColor].CGColor,(id)[self randomColor].CGColor,(id)[self randomColor].CGColor,(id)[self randomColor].CGColor,(id)[self randomColor].CGColor];
    


}
-(UIColor*)randomColor
{
    UIColor*color=[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    return color;




}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
